package cn.kgc.controller.logic.hr;

import cn.kgc.dao.hr.chuchai;
import cn.kgc.dao.hr.qingjia;
import cn.kgc.pojo.User;
import cn.kgc.service.hr.chuchaiService;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author: wang
 * @date: 2022/2/24
 * @description:
 */
@RequestMapping("/export")
@Controller
public class ExcelController {

    @Resource
    private chuchaiService chuchaiService;

    @RequestMapping(value = "/请假")
    public void export(HttpServletResponse response,HttpSession session) throws IOException {
        User user = (User)session.getAttribute("user");
        List<chuchai> chuchais = chuchaiService.selAll(user.getId());
        System.out.printf("------------" + chuchais.toString());
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet = wb.createSheet(user.getName()+"请假记录表");

        HSSFRow row = null;
        row = sheet.createRow(0);//创建第一个单元格
        row.setHeight((short) (26.25 * 20));
        row.createCell(0).setCellValue(user.getName()+"请假记录信息");//为第一行单元格设值

        /*为标题设计空间
         * firstRow从第1行开始
         * lastRow从第0行结束
         *
         *从第1个单元格开始
         * 从第3个单元格结束
         */
        CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 8);
        sheet.addMergedRegion(rowRegion);

      /*CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
      sheet.addMergedRegion(columnRegion);*/

        row = sheet.createRow(1);
        row.setHeight((short) (22.50 * 20));//设置行高
        row.createCell(0).setCellValue("Id");//为第一个单元格设值
        row.createCell(1).setCellValue("请假人");//为第二个单元格设值
        row.createCell(2).setCellValue("职位");//为第三个单元格设值
        row.createCell(3).setCellValue("开始时间");//为第四个单元格设值
        row.createCell(4).setCellValue("结束时间");//为第四个单元格设值
        row.createCell(5).setCellValue("请假类型");//为第四个单元格设值
        row.createCell(6).setCellValue("请假事由");//为第四个单元格设值
        row.createCell(7).setCellValue("审批人");//为第四个单元格设值
        row.createCell(8).setCellValue("申请状态");//为第四个单元格设值


        //设置日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //遍历所获取的数据
        for (int i = 0; i < chuchais.size(); i++) {
            row = sheet.createRow(i + 2);
            chuchai chuchai = chuchais.get(i);
            row.createCell(0).setCellValue(chuchai.getId());
            row.createCell(1).setCellValue(chuchai.getName());
            row.createCell(2).setCellValue(chuchai.getJob());
            row.createCell(3).setCellValue(dateFormat.format(chuchai.getBeginTime()));
            row.createCell(4).setCellValue(dateFormat.format(chuchai.getEndTime()));
            row.createCell(5).setCellValue(chuchai.getType());
            row.createCell(6).setCellValue(chuchai.getReason());
            row.createCell(7).setCellValue(chuchai.getApprover());
            String zhutai="";
            if (chuchai.getState()==0){
                zhutai="审核中";
            }else if(chuchai.getState()==1){
                zhutai="通过";
            }else if(chuchai.getState()==-1){
                zhutai="拒绝";
            }else if(chuchai.getState()==-2){
                zhutai="自己取消";
            }
            row.createCell(8).setCellValue(zhutai);
        }
        sheet.setDefaultRowHeight((short) (16.5 * 20));

        //列宽自适应
        for (int i = 0; i <= 40; i++) {
            sheet.autoSizeColumn(i);
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename=请假记录表");//默认Excel名称
        wb.write(os);
        os.flush();
        os.close();
    }

    @RequestMapping(value = "/出差")
    public void chuchai(HttpServletResponse response,HttpSession session) throws IOException {
        User user = (User)session.getAttribute("user");
        List<qingjia> qingjias = chuchaiService.selAllQj(user.getId());
        System.out.printf("------------" + qingjias.toString());
        HSSFWorkbook wb = new HSSFWorkbook();

        HSSFSheet sheet = wb.createSheet(user.getName()+"出差记录表");

        HSSFRow row = null;
        row = sheet.createRow(0);//创建第一个单元格
        row.setHeight((short) (26.25 * 20));
        row.createCell(0).setCellValue(user.getName()+"出差记录信息");//为第一行单元格设值

        /*为标题设计空间
         * firstRow从第1行开始
         * lastRow从第0行结束
         *
         *从第1个单元格开始
         * 从第3个单元格结束
         */
        CellRangeAddress rowRegion = new CellRangeAddress(0, 0, 0, 8);
        sheet.addMergedRegion(rowRegion);

      /*CellRangeAddress columnRegion = new CellRangeAddress(1,4,0,0);
      sheet.addMergedRegion(columnRegion);*/

        row = sheet.createRow(1);
        row.setHeight((short) (22.50 * 20));//设置行高
        row.createCell(0).setCellValue("Id");//为第一个单元格设值
        row.createCell(1).setCellValue("姓名");//为第二个单元格设值
        row.createCell(2).setCellValue("职位");//为第三个单元格设值
        row.createCell(3).setCellValue("开始时间");//为第四个单元格设值
        row.createCell(4).setCellValue("结束时间");//为第四个单元格设值
        row.createCell(5).setCellValue("交通工具");//为第四个单元格设值
        row.createCell(6).setCellValue("工作地点");//为第四个单元格设值
        row.createCell(7).setCellValue("工作任务");//为第四个单元格设值
        row.createCell(8).setCellValue("借款金额");//为第四个单元格设值
        row.createCell(9).setCellValue("审批人");//为第四个单元格设值
        row.createCell(10).setCellValue("申请状态");//为第四个单元格设值


        //设置日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //遍历所获取的数据
        for (int i = 0; i < qingjias.size(); i++) {
            row = sheet.createRow(i + 2);
            qingjia qingjia = qingjias.get(i);
            row.createCell(0).setCellValue(qingjia.getId());
            row.createCell(1).setCellValue(qingjia.getName());
            row.createCell(2).setCellValue(qingjia.getJob());
            row.createCell(3).setCellValue(dateFormat.format(qingjia.getBeginTime()));
            row.createCell(4).setCellValue(dateFormat.format(qingjia.getEndTime()));
            row.createCell(5).setCellValue(qingjia.getType());
            row.createCell(6).setCellValue(qingjia.getSite());
            row.createCell(7).setCellValue(qingjia.getMission());
            row.createCell(8).setCellValue(qingjia.getMoney());
            row.createCell(9).setCellValue(qingjia.getApprover());
            String zhutai="";
            if (qingjia.getState()==0){
                zhutai="审核中";
            }else if(qingjia.getState()==1){
                zhutai="通过";
            }else if(qingjia.getState()==-1){
                zhutai="拒绝";
            }else if(qingjia.getState()==-2){
                zhutai="自己取消";
            }
            row.createCell(10).setCellValue(zhutai);
        }
        sheet.setDefaultRowHeight((short) (16.5 * 20));

        //列宽自适应
        for (int i = 0; i <= 40; i++) {
            sheet.autoSizeColumn(i);
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment;filename=请假记录表");//默认Excel名称
        wb.write(os);
        os.flush();
        os.close();
    }

}
